using System.Collections.Generic;
using QFramework.Framework.Util;
using UnityEditor;
using UnityEngine;

namespace QFramework.Framework.ResKit
{
    /// <summary>
    ///     资源管理类：享元模式
    /// </summary>
    public class ResMgr : MonoSingleton<ResMgr>
    {
        private const string SimulationModeKey = "simulation mode";

        /// <summary>
        ///     共享资源池
        /// </summary>
        public readonly List<Res.Res> SharedLoadedReses = new();

        public static bool IsSimulationModeLogic
        {
            get
            {
#if UNITY_EDITOR
                return SimulationMode;
#else
                return false;
#endif
            }
        }

        private void OnGUI()
        {
            if (Input.GetKey(KeyCode.F1))
            {
                GUILayout.BeginVertical("box");

                SharedLoadedReses.ForEach(loadedRes =>
                {
                    GUILayout.Label($"Name:{loadedRes.Name} RefCount:{loadedRes.RefCount} State:{loadedRes.State}");
                });

                GUILayout.EndVertical();
            }
        }

#if UNITY_EDITOR
        private static int _simulationMode = -1;

        public static bool SimulationMode
        {
            get
            {
                if (_simulationMode == -1) _simulationMode = EditorPrefs.GetBool(SimulationModeKey, true) ? 1 : 0;

                return _simulationMode != 0;
            }

            set
            {
                _simulationMode = value ? 1 : 0;
                EditorPrefs.SetBool(SimulationModeKey, value);
            }
        }
#endif
    }
}